package com.acxmpp.activity;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.acxmpp.R;
import com.acxmpp.common.data.TempData;

/**
 * @author daisongwan
 * 
 */
public class LoginActivity extends Activity {
	private CheckBox saveUsername = null;
	private CheckBox savePassword = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		saveUsername = (CheckBox) findViewById(R.id.saveUsername);
		savePassword = (CheckBox) findViewById(R.id.savePassword);
		savePassword
				.setOnCheckedChangeListener(new SavePasswordCheckedChangeListener());
		Log.d("LoginActivity", "LoginActivity:onCreate incoked!");
		login();
	}

	/**
	 * 
	 */
	public void login() {

		Log.d("LoginActivity", "LoginActivity:login incoked!");
		ConnectionConfiguration config = new ConnectionConfiguration(
				TempData.LOGIN_SERVIER_IP, TempData.LOGIN_SERVIER_PORT);
		config.setCompressionEnabled(true);
		config.setSASLAuthenticationEnabled(false);
		XMPPConnection connection = new XMPPConnection(config);
		try {
			Log.d("LoginActivity", "before connect.");
			connection.connect();
			System.out.println("Login start");
			connection.login(TempData.LOGIN_USERNAME, TempData.LOGIN_PASSWORD);
			System.out.println("Login successful");
		} catch (Exception e) {
			System.out.println("Login error");
			e.printStackTrace();
		} finally {
			connection.disconnect();
		}

	}

	/**
	 * 
	 */
	class SavePasswordCheckedChangeListener implements OnCheckedChangeListener {

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (isChecked) {
				saveUsername.setChecked(true);
			} else {
				saveUsername.setChecked(false);
			}
		}

	}
}